/**
 * Description: 生成随机字符串
 * File: main.c |Time: 2022-07-11 16:30
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 128

void rand_str(char *dest, size_t length);

int main(int argc, const char *argv[])
{
    char str[SIZE];
    for (int i = 0; i < 10; i++) 
    {
       memset(str, 0, SIZE);
       rand_str(str, 20);
       printf("%s\n", str);
    }
    
    return 0;
}


void rand_str(char *dest, size_t length) {
    char charset[] = "0123456789"
                     "abcdefghijklmnopqrstuvwxyz"
                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    while (length-- > 0) {
        size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
        *dest++ = charset[index];
    }
    *dest = '\0';
}


